/**
 * @author tykon
 */
(function($){
    $.extend({
        /**
         * 在浏览器的不同模式下读取document对象的内容（因为ie有两种模式，一个是和ff等浏览器相同的标准模型，一个是快速模型）
         * getDocument
         */
        getDoc: function(){
            var hasDocType = document.compatMode === 'CSS1Compat', isBorderMode = $.browser.msie && !hasDocType;
            return isBorderMode && document.body || document.documentElement;
        },
        /**
         * 文档可见宽
         */
        getViewportWidth: function(){
            return $.browser.msie && this.getDoc().clientWidth || self.innerWidth//self.innerWidth是指window中的文档部分的宽度;
        },
        
        /**
         * 文档可见高
         */
        getViewportHeight: function(){
            return $.browser.msie && this.getDoc().clientHeight || self.innerHeight;
        },
        getRandomId: function(len){
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
            var string_length = parseInt(len, 10) || 8;
            var randomstring = '';
            for (var i = 0; i < string_length; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum, rnum + 1);
            }
            return randomstring;
        },
        onlyNum: function(event, th){
            var str = "";
            var Letters = "1234567890";
            var i;
            var c
            
            for (i = 0; i < th.value.length; i++) {
                c = th.value.charAt(i);
                if (Letters.indexOf(c) != -1) {
                    str = str + c;
                }
            }
            th.value = str;
        },
        getBackgroundPositionX: function(){
            var x;
            try {
                x = $(this).css('background-positionX');
                if (!x) {
                    x = $(this).css('background-position').split(" ")[0];
                }
            } 
            catch (e) {
            }
            return x;
        },
        getBackgroundPositionY: function(){
            var y;
            try {
                y = $(this).css('background-positionY');
                if (!y) {
                    y = $(this).css('background-position').split(" ")[1];
                }
            } 
            catch (e) {
            }
            return y;
        },
        isEmail: function(email){
            var regex = /^[a-zA-Z0-9]+([\._-][a-zA-Z0-9]+)*@(([a-zA-Z0-9]+([\.-][a-zA-Z0-9]+)?)+\.)+[a-zA-Z]+$/;
            return regex.test(email);
        },
        getCookie: function(key){
            return $.cookie(cookie);
        },
        setCookie: function(key, value, expires){
            $.cookie(key, value, {
                expires: 5000
            });
        },
        deleteCookie: function(key){
            $.cookie(key, '', {
                expires: -1
            });
        },
        addStyleSheet: function(href){
            var herfs = href.split(',');
            for (var i = 0; i < herfs.length; i++) {
                $('<link href="' + herfs[i] + '" rel="stylesheet" type="text/css" />').appendTo('head');
            }
        },
        addJsSource: function(href, callBack){
            var herfs = href.split(',');
            for (var i = 0; i < herfs.length; i++) {
                $.getScript(herfs[i], callBack);
            }
        },
        getPage: function(){
        
        },
        ajaxPage: function(){
        
        },
        isPhone: function(s){
            $s = $("#" + s);
            var patrn = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
            var regy = /^1(3[4-9]|5[012789]|8[78])\d{8}$/;
            var regl = /^((130|131|132|155|156|186)+\d{8})$/;
            var regd = /^((133|153|189)+\d{8})$/;
            if (patrn.test($s.val()) || regy.test($s.val()) || regl.test($s.val()) || regd.test($s.val())) {
                return true;
            }
            else {
                return false;
            }
        },
        //是否是数组
        isArray: function(o){
            return Object.prototype.toString.call(o) === '[object Array]';
        },
        array2String: function(array){
            var output = "";
            if (array) {
                output += "[";
                for (var i in array) {
                    val = array[i];
                    switch (typeof val) {
                        case ("object"):
                            if (val[0]) {
                                output += this.array2String(val) + ",";
                            }
                            else {
                                output += this.object2String(val) + ",";
                            }
                            break;
                            
                        case ("string"):
                            
                            output += "'" + val + "',";
                            
                            break;
                            
                        default:
                            output += val + ",";
                    }
                }
                output = output.substring(0, output.length - 1) + "]";
            }
            return output;
        },
        object2String: function(obj){
            var val, output = "";
            if (obj) {
                output += "{";
                for (var i in obj) {
                    val = obj[i];
                    switch (typeof val) {
                        case ("object"):
                            if (val[0]) {
                                output += '"' + i + '":' + this.array2String(val) + ',';
                            }
                            else {
                                output += '"' + i + '":[],';
                            }
                            break;
                        case ("string"):
                            output += '"' + i + '":"' + val + '",';
                            break;
                        default:
                            output += '"' + i + '":' + val + ',';
                    }
                }
                output = output.substring(0, output.length - 1) + "}";
            }
            return output;
        },
        string2Object: function(string){
            eval("var result = " + (string));
            return result;
        },
        string2Array: function(string){
            eval("var result = " + (string));
            return result;
        },
        min: function(){
            var min = obj[0];
            var len = obj.length;
            for (var i = 1; i < len; i++) {
                if (obj[i] < min) {
                    min = obj[i];
                }
            }
            return min;
        },
        max: function(){
            var max = obj[0];
            var len = obj.length;
            for (var i = 1; i < len; i++) {
                if (obj[i] > max) {
                    max = obj[i];
                }
            }
            return max;
        }
    });
    
    $.fn.extend({
        setCheck: function($who){
            $status = $(this).attr('checked');
            return $who.each(function(){
                this.checked = $status;
            });
        },
        checkAll: function(){
            return this.each(function(){
                this.checked = true;
            });
        },
        unCheck: function(){
            return this.each(function(){
                this.checked = false;
            });
        },
        negateCheck: function(){
            return this.each(function(){
                this.checked = this.checked ? false : true;
            });
        },
        isHasChecked: function(){
            $flag = false;
            $(this).each(function(){
                if ($(this).attr('checked')) 
                    $flag = true;
            });
            return $flag;
        },
        checkedItemsById: function(){
            var $items = "";
            $(this).each(function(i){
                if (this.checked) 
                    $items += $(this).attr('id') + ',';
            });
            return $items.substring(0, $items.length - 1);
        },
        collectGarbage: function(){
            $("*", this).andSelf().each(function(){
                $.event.remove(this);
                $.removeData(this);
            });
            $(this).html('');
        },
        enterEvent: function(fun){
            $(this).keypress(function(event){
                event.stopPropagation();
                event.preventDefault();
                if (event.keyCode == 13) {
                    fun();
                }
            });
        }
    })
})(jQuery)
